package a66weding.com.jiehuntong.activity;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SimpleBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;

public class ContactUsActivity extends Activity implements View.OnClickListener, OnNetResultListener {
    private boolean isShow = false;
    private ImageButton imgbtn_show_phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_us);
        Intent intent = getIntent();
        if (intent != null) {
            isShow = intent.getBooleanExtra("isShow", false);
        }
        initView();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void initView() {
        imgbtn_show_phone = (ImageButton) findViewById(R.id.imgbtn_show_phone);
        if (isShow) {
            imgbtn_show_phone.setBackground(getResources().getDrawable(R.mipmap.show_phone_open));
        } else {
            imgbtn_show_phone.setBackground(getResources().getDrawable(R.mipmap.show_phone_close));
        }
        imgbtn_show_phone.setOnClickListener(this);
//        头部导航部分
        TextView tv_tools_dayplan_backcontent = (TextView) findViewById(R.id.tv_tools_dayplan_backcontent);
        LinearLayout llay_toolsheader_back = (LinearLayout) findViewById(R.id.llay_toolsheader_back);
        TextView tv_tools_dayplan_title = (TextView) findViewById(R.id.tv_tools_dayplan_title);
        TextView tv_tools_dayplan_addtype1 = (TextView) findViewById(R.id.tv_tools_dayplan_addtype1);
        ImageView img_tools_dayplan_backcontent = (ImageView) findViewById(R.id.img_tools_dayplan_backcontent);

        img_tools_dayplan_backcontent.setOnClickListener(this);
        tv_tools_dayplan_backcontent.setText("   ");
        tv_tools_dayplan_title.setText("联系我们");
        tv_tools_dayplan_addtype1.setText("保存");
        tv_tools_dayplan_addtype1.setVisibility(View.VISIBLE);
        tv_tools_dayplan_backcontent.setOnClickListener(this);
        llay_toolsheader_back.setOnClickListener(this);
        tv_tools_dayplan_backcontent.setOnClickListener(this);
        tv_tools_dayplan_addtype1.setOnClickListener(this);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llay_toolsheader_back:
                this.finish();
                break;
            case R.id.img_tools_dayplan_backcontent:
                this.finish();
                break;
            case R.id.imgbtn_show_phone:
                if (isShow) {
                    isShow = false;
                    imgbtn_show_phone.setBackground(getResources().getDrawable(R.mipmap.show_phone_close));
                } else {
                    imgbtn_show_phone.setBackground(getResources().getDrawable(R.mipmap.show_phone_open));
                    isShow = true;
                }
                break;
            case R.id.tv_tools_dayplan_addtype1:
                saveDate2Net();
                break;
        }
    }

    /**
     * WeddingMobileApi/SaveMarryCardByUid?Token={Token}&uid={uid}{key}&value={value}
     */
    private void saveDate2Net() {
        int uId= DataWareHourse.getInstance().getmUser().getData().get(0).getI_Id();
        HttpRequestBean httpRequestBean = new HttpRequestBean();
        httpRequestBean.setListener(this);
        httpRequestBean.setClazz(SimpleBean.class);
        httpRequestBean.setRequestMethod(RequestCode.POSTEQUEST);
        httpRequestBean.setUrl(RequestCode.SURL + RequestCode.CHANGEINFO + uId + "&key=10" + "&value=" + isShow);
        HttpHandleUtil.executeCommonGet(this, httpRequestBean);
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if (data instanceof SimpleBean) {
            SimpleBean spb = (SimpleBean) data;
            CUtils.showToast(spb.getMsg());
            if (spb != null && spb.getResCode() != null) {
                if (spb.getResCode().equals("0000")) {
                    CUtils.showToast(spb.getMsg());
                    Intent intent = new Intent();
                    intent.putExtra("isShow", isShow);
                    setResult(99, intent);
                    finish();
                }else {
                    CUtils.showToast(spb.getMsg());
                }
            }
        }
}

    @Override
    public void onError(ErrorBean errorBean) {
        CUtils.showToast("数据请求失败");
    }
}